package com.hzh.page.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzh.page.dao.AuthenticationMenuMapper;
import com.hzh.page.dao.AuthenticationRoleMenuMapper;
import com.hzh.page.pojo.AuthenticationRoleMenuEntity;
import com.hzh.page.service.AuthenticationRoleMenuService;
import com.hzh.page.vo.AuthenticationRoleMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 胡泽华
 */
@Service
public class AuthenticationRoleMenuServiceImpl extends ServiceImpl<AuthenticationRoleMenuMapper, AuthenticationRoleMenuEntity> implements AuthenticationRoleMenuService {
    @Resource
    AuthenticationMenuMapper menuMapper;
    @Autowired
    private AuthenticationRoleMenuService roleMenuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleMenus(AuthenticationRoleMenuVO authenticationRoleMenuVO) {
        Integer roleId = authenticationRoleMenuVO.getId();
        List<String> rights = authenticationRoleMenuVO.getRights();
        List<Integer> roleIdList = menuMapper.getRoleIdsByRights(rights);
        List<Integer> menuIds = roleIdList;
        baseMapper.delete(new QueryWrapper<AuthenticationRoleMenuEntity>().eq("role_id", roleId));
        List<AuthenticationRoleMenuEntity> collect = menuIds.stream().map(menuId -> {
            AuthenticationRoleMenuEntity roleMenuEntity = new AuthenticationRoleMenuEntity();
            roleMenuEntity.setRoleId(roleId);
            roleMenuEntity.setMenuId(menuId);
            return roleMenuEntity;
        }).collect(Collectors.toList());
        roleMenuService.saveBatch(collect);


    }

}
